package com.tokenplus.thai.changepassword

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.R
import com.tokenplus.thai.adata.login.ReqChangePassword
import com.tokenplus.thai.aviewmodel.LoginViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.databinding.ActivityPasswordNewInputBinding
import com.tokenplus.thai.enums.PageTagEnum
import com.tokenplus.thai.extend.startToHomeActivity

/** 修改密码-输入新密码页 */
@SuppressLint("CustomSplashScreen")
class PasswordNewInputActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityPasswordNewInputBinding

    //登录viewModel
    private val mLoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoginViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.password_new_input.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityPasswordNewInputBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //监听live data 返回
            onHandleLiveDataCallback()

            //点击确认
            mActivityBinding.submitLoginInputMobile.setOnClickListener {
                doChangePassword()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        //检查设备回调
        mLoginViewModel.onChangePasswordLiveData.observe(this) {
            if (it.isSuccess) {
                mActivityBinding.inputOldPassword.setContent("")
                mActivityBinding.inputNewPassword.setContent("")
                mActivityBinding.inputNewPasswordConfirm.setContent("")

                Toast.makeText(
                    this@PasswordNewInputActivity,
                    getString(R.string.my_change_password_success),
                    Toast.LENGTH_SHORT
                ).show()

                Handler(Looper.getMainLooper()).postDelayed({
                    startToHomeActivity()
                }, 1000)
            }
        }
    }

    /** 去修改密码 */
    private fun doChangePassword() {
        val zjc = intent.getStringExtra("zjc").toString()
        val oldPassword = mActivityBinding.inputOldPassword.getContent()
        val newPassword = mActivityBinding.inputNewPassword.getContent()
        val newPasswordConfirm = mActivityBinding.inputNewPasswordConfirm.getContent()

        val con1 = oldPassword.length >= 8
        val con2 = newPassword.length >= 8
        val con3 = newPasswordConfirm.length >= 8
        val con4 = newPassword == newPasswordConfirm
        val con5 = zjc.length >= 4

        if (!con1) {
            Toast.makeText(
                this@PasswordNewInputActivity,
                getString(R.string.login_wrong_input_please_check_and_reenter),
                Toast.LENGTH_SHORT
            ).show()
            return
        }

        if (!con2) {
            Toast.makeText(
                this@PasswordNewInputActivity,
                getString(R.string.login_wrong_input_please_check_and_reenter),
                Toast.LENGTH_SHORT
            ).show()
            return
        }

        if (!con3) {
            Toast.makeText(
                this@PasswordNewInputActivity,
                getString(R.string.login_wrong_input_please_check_and_reenter),
                Toast.LENGTH_SHORT
            ).show()
            return
        }

        if (!con4) {
            Toast.makeText(
                this@PasswordNewInputActivity,
                getString(R.string.login_wrong_input_please_check_and_reenter),
                Toast.LENGTH_SHORT
            ).show()
            return
        }

        if (!con5) {
            Toast.makeText(
                this@PasswordNewInputActivity,
                getString(R.string.login_wrong_input_please_check_and_reenter),
                Toast.LENGTH_SHORT
            ).show()

            Handler(Looper.getMainLooper()).postDelayed({
                finish()
            }, 1000)
            return
        }

        mLoginViewModel.changePassword(
            ReqChangePassword(
                oldPassword = oldPassword,
                newPassword = newPassword,
                zjc = zjc
            )
        )
    }
}